<?php echo $this->element('admin_event_search_filter',array('categories' => $categories));?>
<div class="clear"></div>
<div class="events index">
<?php
echo "<div class='switcher'>";
echo __("Switch to ").$html->link("Events Settings",array("plugin"=>"events","controller"=>"event_config","action"=>"setting","Event"));
echo "&nbsp;&nbsp;".$html->link("Event Categories",array('plugin'=>'events','controller'=>'event_categories','action'=>'index'));
echo "</div>";

$index->setData($events, 'Event');

$index->showFields(array('id','title',array('event_category_id'=>__('Event Category')),array("event_from"=>__("Event From/To")),array('listing_id'=>__('Listing')),array('user_id'=>__('User'))));
$index->sortableFields(array('title','event_from','id'));

$index->addColumn('listing_id','showListing');
$index->addColumn('event_category_id','showCategory');
$index->addColumn('event_from','showEventDates');
$index->addColumn('user_id','showEventUser');

function showListing($record)
{
	$html = ClassRegistry::getObject('view')->loaded['html'];
	$listings = ClassRegistry::getObject('view')->viewVars['listings'];
	if(isset($listings[$record['Event']['listing_id']]) && !empty($listings[$record['Event']['listing_id']]))
	{
		return $html->link($listings[$record['Event']['listing_id']]['Listing']['title'], $html->url(array("plugin"=>null,"controller"=>"listings","action"=>"view",$record['Event']['listing_id']), true));
	}
	return __("N/A");
}
function showCategory($record)
{
	$html = ClassRegistry::getObject('view')->loaded['html'];
	return $html->link($record['EventCategory']['name'],array('plugin'=>'events','controller'=>'event_categories','action'=>'admin_view',$record['EventCategory']['id']));
}
function showEventUser($record)
{
	if (isset($record['Event']['poster']) && $record['Event']['poster'] != null) 
	{
		$html = ClassRegistry::getObject('view')->loaded['html'];
		return $html->link($record['Event']['poster']['username'], array("plugin"=>"users",'controller' => 'users', 'action' => 'admin_view', $record['Event']['poster']['id']));
	} else {
		return __("N/A");
	}
}
function showEventDates($record)
{
	$eventWhen = date("Y-m-d",strtotime($record['Event']['event_from']));
	if(isset($record['Event']['event_to']) && $record['Event']['event_to']!=null)
	{
		$eventWhen = $eventWhen . ' - ' . date("Y-m-d",strtotime($record['Event']['event_to']));
	}
	return $eventWhen;
}

function publish_unpublish($model,$record)
{
	$action = array();
	if($record[$model]['status'] == Event::PUBLISH)
	{
		$action['title'] = __('Unpublish');
		$action['url'] = '/admin/events/events/changeStatus/'.$record[$model]['id'];
		$action['confirmMessage'] = __('Are you sure, you want to unpublish this event ?');
	} else {
		$action['title'] = __('Publish');
		$action['url'] = '/admin/events/events/changeStatus/'.$record[$model]['id'];
	}
	return $action;
}

$index->setAction(__('View'), array('action'=>'view', ':Event.id'));
$index->setAction(__('Edit'), array('action'=>'edit', ':Event.id'));
$index->setAction(__('Delete'), array('action'=>'delete', ':Event.id'),$this->helpers,__('Are you sure, you want to delete this event ?'));
$index->setAction(null, null,$this->helpers,false,true,'publish_unpublish');

$index->addButton(__("Delete Selected"),array("action"=>"bulkDelete",'admin'=>true,'plugin'=>'events','controller'=>'events'),array("onSubmit"=>"if(!confirm('".__("Are you sure?")."')){return false;}"));
$index->addButton(__("Unpublish Selected"),array("action"=>"bulkUnpublish",'admin'=>true,'plugin'=>'events','controller'=>'events'));
$index->addButton(__("Publish Selected"),array("action"=>"bulkPublish",'admin'=>true,'plugin'=>'events','controller'=>'events'));

echo $index->display($events);
?>
</div>
